﻿Imports System.Windows.Forms

Public Class Dialog2
    Dim tempNames As New Specialized.StringCollection
    Dim change As Boolean = False
    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
        If Not TextBox1.Text = "" Then
            change = True
        End If
        If change Then
            If MsgBox("Save changes?", MsgBoxStyle.OkCancel, "Save?") = MsgBoxResult.Ok Then
                Me.DialogResult = System.Windows.Forms.DialogResult.OK
                addPlayers()
                My.Settings.Playernames = tempNames
                Me.Close()
            End If
        Else
            Me.Close()
        End If
    End Sub

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
        If change Then
            If MsgBox("Cancel all changes?", MsgBoxStyle.OkCancel, "Cancel?") = MsgBoxResult.Ok Then
                Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
                Me.Close()
            End If
        End If
    End Sub

    Private Sub Dialog2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        For Each s As String In My.Settings.Playernames
            tempNames.Add(s)
        Next
        populatePlayerList()
    End Sub

    Private Sub populatePlayerList()
        ListBox1.Items.Clear()
        For Each s As String In tempNames
            ListBox1.Items.Add(s)
        Next
    End Sub

    Private Sub ListBox1_SelectedValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedValueChanged
        If Not ListBox1.SelectedItem Is Nothing Then
            tempNames.Remove(sender.SelectedItem.ToString)
            populatePlayerList()
            change = True
        End If
    End Sub

    Private Sub addPlayers()
        If TextBox1.Lines.Count > 0 Then
            For Each s As String In TextBox1.Lines
                If Not s = "" Then
                    tempNames.Add(s)
                End If
            Next
        End If
    End Sub

    Private Sub btnClearPlayers_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClearPlayers.Click
        If tempNames.Count > 0 Then
            change = True
        End If
        tempNames.Clear()
        populatePlayerList()
    End Sub
End Class
